Skip to content

Read structs with defaults that hold a Validator without a valid default - #38

Draft
clockblocker wants to merge 1 commit into
default-val-fixesfrom
fix-700-deferred-defaults-next
Draft

clockblocker wants to merge 1 commit into
default-val-fixesfrom
fix-700-deferred-defaults-next

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes munich-data-engineering/reflect-cpp#700 (next iteration of munich-data-engineering#780): allows reading and generating JSON Schema for structs that mix rfl::DefaultVal / rfl::DefaultIfMissing with rfl::Validator fields whose default value is invalid, while keeping rfl::Validator trivially copyable.

Example

using Foods = rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>>;

struct Person {
  rfl::DefaultVal<std::string> name = "Homer Simpson";
  Foods favorite_foods;  // Foods{} throws (size 0 < 1)
};

// Before (main): fails before looking at input ("Could not default-construct Person...")
// After:
rfl::json::read<Person>(R"({"favorite_foods": ["donuts"]})");
// -> ok: name="Homer Simpson", favorite_foods={"donuts"}

rfl::json::read<Person>(R"({})");
// -> error: Field named 'favorite_foods' not found.

rfl::json::read<Person, rfl::DefaultIfMissing>(R"({})");
// -> error: Field named 'favorite_foods' not found, and its default value is invalid: ...

rfl::json::to_schema<Person>();
// -> "name" keeps "default": "Homer Simpson"; "favorite_foods" is in "required"
// Person{} outside read() still throws.

How it works

  • When T{} throws during read_struct_with_default, the parser retries T{} inside a thread-local deferral scope where Validator() keeps its invalid default and increments a counter instead of throwing.
  • After construction, the parser walks T to confirm it holds at least as many invalid Validators as were deferred; after reading, it walks every missing field's default and fails if any invalid Validator or uncheckable opaque type remains.
  • Validator keeps defaulted copy/move constructors (stays trivially copyable; zero copy-path overhead).

Known limits

  • A default member initializer that reads a deferred Validator (e.g. int b = a.value() * 2;) observes its invalid value.
  • A discarded temporary Validator (e.g. int port = Positive().value();) fails the read unless another invalid Validator copy offsets the count.
  • A Validator inside an opaque class the walker cannot inspect fails the read (as on main), and a missing field of such an opaque type also fails when deferral is active.
  • A try/catch fallback inside a member initializer receives no exception while deferral is active, so that field must be supplied in the input.

AI-authored. Base: main (after munich-data-engineering#786).

@clockblocker
clockblocker force-pushed the fix-700-deferred-defaults-next branch from 57cb03d to ac217e3 Compare November 17, 2026 08:30
@clockblocker
clockblocker force-pushed the fix-700-deferred-defaults-next branch 2 times, most recently from 288f83a to 9afb290 Compare November 17, 2026 18:57
@clockblocker
clockblocker force-pushed the fix-700-deferred-defaults-next branch from 9afb290 to fc8e765 Compare November 21, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant